﻿using System;
using System.Collections.Generic;
using System.Linq;
using Simple.Data;

namespace Model.User
{
    public class Person
    {
        protected Int32 id;
        protected String firstName = null;
        protected String lastName = null;
        protected String password = null;
        protected String email = null;
        protected String picturePath = null;
        protected Int32 role = -1;

        protected Person()
        {
        }

        protected Person(Int32 id)
        {
            this.id = id;
        }

        public static Person get(Int32 id)
        {
            var db = Database.Open();
            
            if (db.Person.GetCountById(id) > 0)
                    return new Person(id);

            return null;
        }

        public static Person get(String email)
        {
            var db = Database.Open();
            
            var person = db.Person.FindAllByEmail(email).FirstOrDefault();

            if (person != null)
                return new Person(person.id);            

            return null;
        }

        public static Person create(String firstName, String lastName, String password,
                                    String email, String picturePath, int role = 2)
        {
            if ((firstName == null) || (lastName == null) || 
                (password == null) || (email == null))
                return null; //throw exception

            var db = Database.Open();
            var p = db.Person.FindAllByEmail(email).FirstOrDefault();

            if (p == null)
            {
                var person = db.Person.Insert(firstName: firstName, LastName: lastName, Password: password,
                    Email: email, PicturePath: picturePath, Role: role);

                return new Person(person.id);
            }//else throw exception

            return null;
        }

        public static int count()
        {
            return Database.Open().Person.GetCount();
        }

        public void delete()
        {
            Database.Open().Person.Delete(id: id);
        }

        public int getId()
        {
            return id;
        }

        public String getFirstName()
        {
            if (firstName == null)
            {
                var person = Database.Open().Person.Get(id);
                firstName = person.firstName;
            }

            return firstName;
        }

        public void setFirstName(String value)
        {
            firstName = value;
            Database.Open().Person.UpdateByID(ID: id, FirstName: firstName);
        }

        public String getLastName()
        {
            if (lastName == null)
            {
                var person = Database.Open().Person.Get(id);
                lastName = person.lastName;
            }

            return lastName;
        }

        public void setLastName(String value)
        {
            lastName = value;
            Database.Open().Person.UpdateByID(ID: id, LastName: lastName);
        }

        public String getPassword()
        {
            if (password == null)
            {
                var person = Database.Open().Person.Get(id);
                password = person.password;
            }

            return password;
        }

        public void setPassword(String value)
        {
            password = value;
            Database.Open().Person.UpdateByID(ID: id, Password: password);
        }

        public String getEmail()
        {
            if (email == null)
            {
                var person = Database.Open().Person.Get(id);
                email = person.email;
            }

            return email;
        }

        public void setEmail(String value)
        {
            email = value;
            Database.Open().Person.UpdateByID(ID: id, Email: email);
        }

        public String getPicturePath()
        {
            if (picturePath == null)
            {
                var person = Database.Open().Person.Get(id);
                picturePath = person.picturePath;
            }

            return picturePath;
        }

        public void setPicturePath(String value)
        {
            picturePath = value;
            Database.Open().Person.UpdateByID(ID: id, PicturePath: picturePath);
        }

        public Int32 getRole()
        {
            if (role == -1)
            {
                var person = Database.Open().Person.Get(id);
                role = person.role;
            }

            return role;
        }

        public void setRole(Int32 value)
        {
            role = value;
            Database.Open().Person.UpdateByID(ID: id, Role: role);
        }


     /*   public void sendMail ()        
        {
            
        }

        public void addTopic ()        
        {
            
        }

        public void deleteTopic ()        
        {
            
        }

        public void addPost ()        
        {
            
        }

        public void deletePoat ()        
        {
            
        }

        public void delete ()        
        {
            
        }

        public void receiveMail(Mail mail)        
        {
            
        }*/
    }
}